package net.zhuike.novel.app.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;

import net.zhuike.novel.app.R;

import framework.common.baseui.UeBaseActivity;
import framework.utils.NetWorkUtil;

/**
 * Created by admin on 2017/2/16.
 */
public class WebViewActivity extends UeBaseActivity implements View.OnClickListener{
    private ProgressBar pb;
    private WebView webView;
    private ImageView failed_img;
    String url="";
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_webiew);
        context=this;
        setTitleText(getIntent().getStringExtra("title"));
        hiddenFooter();
        showGoBackBtn();
        pb = (ProgressBar) findViewById(R.id.pb);
        pb.setMax(100);
        url=getIntent().getStringExtra("url");
        failed_img=(ImageView) findViewById(R.id.failed_img);
        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebChromeClient(new WebViewClient() );

        if(NetWorkUtil.isNetworkAvaliable(context)){
            webView.setWebViewClient(new android.webkit.WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;//ture为在webview中打开

                }
                @Override
                public void onReceivedError(WebView view, int errorCode,
                                            String description, String failingUrl) {
                    // TODO Auto-generated method stub
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    failed_img.setVisibility(View.VISIBLE);

                }
            });
            webView.loadUrl(url);
        }else{
            failed_img.setVisibility(View.VISIBLE);
        }

    }
    private class WebViewClient extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            pb.setProgress(newProgress);
            if(newProgress==100){
                pb.setVisibility(View.GONE);
            }
            super.onProgressChanged(view, newProgress);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.failed_img:
                webView.loadUrl(url);
                break;
        }
    }
}
